public class StudentManager {
    //处理学生、课程、分数的增加、删除、修改、查询，即存储的问题

    private Student[] students;
    private Course[] courses;
    private Grade[] grades;

    //分别为三个数组增加一个计数器，让它始终指向下一个待插入的数组元素的位置
    private int studentCount = 0;
    private int courseCount = 0;
    private int gradeCount = 0;

    public StudentManager() {
    }

    public StudentManager(int studentCount, int courseCount, int gradeCount) {
        this.students = new  Student[studentCount];
        this.courses = new Course[courseCount];
        this.grades = new Grade[gradeCount];
//        this.studentCount = 0;
//        this.courseCount = 0;
//        this.gradeCount = 0;
    }

    //根据ID查询学生
    public Student findStudentById(String studentId) {
        for (int i = 0; i < students.length; i++) {
            if(students[i] != null && students[i].getStudentId().equals(studentId)){
                return students[i];
            }
        }
        return null;
    }


    //增加一个学生
    public void addStudent(Student student) {

        //判断学生是否重复，如果重复，拒绝插入
        if(findStudentById(student.getStudentId()) != null){
            System.out.println("该学生已经存在，你不能添加。");
            return;
        }

        //判断数组容量是否达到上限，如果达到，拒绝插入
        if(studentCount == students.length){
            System.out.println("学生数组的容量已经达到上限，你不能添加。");
            return;
        }

        students[studentCount] = student;
        studentCount++;
        System.out.println("学生添加成功：" + student.toString());
    }


    //根据课程编号来查询课程，并且返回
    public Course findCourseById(String courseId) {

        for (int i = 0; i < courses.length; i++) {
            if(courses[i] != null && courses[i].getCourseId().equals(courseId)){
                return courses[i];
            }
        }
        return null;
    }


    //增加一门课程
    public void addCourse(Course course) {

        //判断课程是否有重复，如果重复，拒绝插入
        if(findCourseById(course.getCourseId()) != null){
            System.out.println("该课程已经存在，不能插入。");
            return;
        }

        //判断当前课程数组的容量是否已经达到上限，如果达到，那么也要拒绝插入
        if(courseCount >= courses.length){
            System.out.println("课程数组已经达到上限，不能插入。");
            return;
        }

        courses[courseCount] = course;
        courseCount++;
        System.out.println("课程添加成功：" +  course.toString());

    }


    //添加分数
    public void addGrade(String studentId, String courseId, double usualScore,double finalScore) {
        Student student = findStudentById(studentId);
        Course course = findCourseById(courseId);

        //这是判断学生或者课程是否存在
        if (student == null || course == null) {
            System.out.println("学生或者课程不存在，不能录入分数。");
            return;
        }

        //判断分数数组的容量是否达到上限
        if(gradeCount >= grades.length){
            System.out.println("分数数组达到上限，不能录入分数。");
            return;
        }


        Grade grade = new Grade(student, course, usualScore, finalScore);
        grades[gradeCount] = grade;
        gradeCount++;
        System.out.println("成功录入分数：" + grade.toString());
    }


    //根据学号查询某同学的的所有成绩
    public void queryStudentGrades(String studentId) {

        Student student = findStudentById(studentId);
        if(student == null){
            System.out.println("该学生不存在。");
            return;
        }

        System.out.println(student.getName() + "的成绩单如下：");

        boolean flag = false;
        for (int i = 0; i < gradeCount; i++) {
            if(grades[i].getStudent() == student){
//                if(grades[i].getStudent().getStudentId().equals(studentId)){
                System.out.println(grades[i].toString());
                flag = true;
            }
        }

        if(!flag){
            System.out.println("该学生目前没有成绩记录。");
        }

    }

    //查询某学生的平均成绩，平均成绩是指如果该学生修多门课，那么平均就是将该门课的总分求和除以修的课程数量
    public double calculateAverageScore(String studentId) {
        Student student = findStudentById(studentId);
        if(student == null){
            System.out.println("该学生不存在。");
            return 0;
        }

        //定义一个保存部分的局部变量，以及统计课程数量的局部变量
        double total = 0;
        int count = 0;


        //遍历数组并且累计各门课程的总评分数，以及计算课程的数量
        for (int i = 0; i < gradeCount; i++) {
            if(grades[i].getStudent() == student){
                total +=  grades[i].getTotalScore();
                count++;
            }
        }

        return count > 0 ? total / count : 0;

    }








}
